<template>
  <div>
      <!-- Start header -->
        <header class="top-header">
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
                <a class="navbar-brand" href="/"><img src="../assets/img/logo.png" alt="image"></a>
                <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                    <span class="navbar-toggler-icon"></span>
                    <span class="navbar-toggler-icon"></span>   
                </button>

                <div class="collapse navbar-collapse justify-content-end" id="navbarTogglerDemo02">
                    <ul class="navbar-nav mr-auto  mt-2 mt-md-0">
                        <li class="nav-item active"><a class="nav-link active" href="index.html">Home</a></li>
                        <li class="nav-item"><a class="nav-link" href="about.html">Aboutus</a></li>
                        <li class="nav-item"><a class="nav-link" href="services.html">Services</a></li>
                        <li class="nav-item"><a class="nav-link" href="#">Blog</a></li>
                        <li class="nav-item"><a class="nav-link" href="contact.html">Contact us</a></li>
                        <li class="nav-item" v-show="username"><a class="nav-link" href='/cart_detail'>购物车</a></li>
                        <li class="nav-item" v-show="username"><a class="nav-link" href='/user_detail'>{{username}}</a></li>
                        <li class="nav-item" v-if ='uid'><a class="nav-link active" style="background:#f2184f;color:#fff;width=70px;" href="" @click="logout">登出</a></li>
                        <li class="nav-item" v-else><a class="nav-link active" style="background:#00BFFF;color:#fff;" :href="'http://127.0.0.1:8082/login/?jwt='+localjwt">登录</a></li>
                        <!-- <li class="nav-item" v-else><a class="nav-link active" style="background:#00BFFF;color:#fff;" href='/login'>登录</a></li> -->
                        <li class="nav-item" v-if="!uid"><a class="nav-link" href='/register'>注册</a></li>
                        <li class="nav-item" v-if='type>=1'><a class="nav-link" href='/super_user'>超级管理</a></li>
                    </ul>
                </div>
            </nav>
        </header>
        <!-- End header -->
  </div>
</template>

<script>
import axios from 'axios'
export default {
    data(){
        return{
            uid:'',
            localjwt:localStorage.getItem('jwt'),
            username:'',
            type:'',
            jwt:'',
            value1: 0,
            option1: [
                { text: 'Home', value: 0 },
                { text: '1', value: 1 },
                { text: '2', value: 2 },
            ],
        }
    },
    created(){
        this.get_jwt()
        this.another_login()
        this.jwt = sessionStorage.getItem('jwt')  
        this.is_login()
        this.uid=sessionStorage.getItem('uid')
        this.username=sessionStorage.getItem('username')
        this.type = sessionStorage.getItem('type')  
        console.log(this.localjwt)

    },

    methods:{
        // 获取携带过来的jwt
        get_jwt(){
            var jwt =this.$route.query.jwt
            if(jwt){
                console.log(jwt)
                sessionStorage.setItem('jwt',jwt)
                localStorage.setItem('jwt',jwt)
                console.log(window.location.host+window.location.pathname)
                window.location.href=window.location.pathname
                this.is_login()
            }
        },
        is_login(){
            if (!this.jwt){
                console.log('未登录')
            }else{
                axios.get('http://127.0.0.1:5000/user/',{
                    params:{jwt:this.jwt}
                }).then(res=>{
                    console.log(res.data)
                    // 判断jwt是否符合
                    if(res.data.code==200){
                        sessionStorage.setItem('uid',res.data.user_data.uid) 
                        sessionStorage.setItem('username',res.data.user_data.username) 
                        sessionStorage.setItem('type',res.data.user_data.type) 

                    }else{
                        this.logout()
                        window.location.href='/'
                    }
                })
            }
        },
        logout(){
            sessionStorage.removeItem('uid')
            sessionStorage.removeItem('username')
            sessionStorage.removeItem('type')
            sessionStorage.removeItem('jwt')
            this.$router.push({name:'index',query:{}})
        },
        another_login(){
            var another_id = this.$route.query.another_id;
            var user_id = this.$route.query.uid;
            var type = this.$route.query.type;
            if (another_id){
				// 自动帮用户登录
				sessionStorage.setItem('username',another_id)
				sessionStorage.setItem('uid',user_id)
				sessionStorage.setItem('type',type)
                this.$router.push({name:'index',query:{}})
			}
        }
    }
}
</script>

<style>
ul{
    margin-left: 35%;
    
}

.justify-content-end li{
    text-align: center;
}


</style>